﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Ls.Web.Admin.Areas.Sys.Models;
using Ls.Utils;
using Ls.Web.Framework;
using System.Dynamic;
using Microsoft.AspNetCore.Authorization;
using Ls.Web.Framework.Permissions;

namespace Ls.Web.Admin.Areas.Sys.Controllers
{
    [Authorize]
    [Area("sys")]
    public class MenuController : BaseController
    {
        private MenuService menuService = new MenuService();
        /// <summary>
        /// 菜单管理
        /// </summary>
        /// <returns></returns>
        [Permission("Sys.Menu.Index")]
        public IActionResult Index()
        {
            ViewData["Title"] = "菜单管理";
            return View();
        }

        /// <summary>
        /// 菜单管理
        /// </summary>
        /// <returns></returns>
        [Permission("Sys.Menu.Index")]
        public IActionResult Indexajax() {
            string name = CommonHelper.ToString(Request.Form["k"]);
            string where = "";
            if (name.IsNotNullOrEmpty())
            {
                where = " (name like '%" + name.Safe() + "%' or url like '%" + name.Safe() + "%' or permission like '%" + name.Safe() + "%')";
            }
            var menuList = menuService.GetList<dynamic>(where, "menu_id,name,icon,url,parent_id,sort,permission,menu_position,menu_type,js_method", "sort");
            List<object> rows = new List<object>();
            foreach (var d in menuList)
            {
                rows.Add(new { d.menu_id, iconCls = d.icon, d.parent_id, d.name, d.url, orderNum = d.sort, permission = d.permission, d.menu_position, d.menu_type,d.js_method });
            }
            return Json(rows);
        }
        /// <summary>
        /// 获取当前用户的权限菜单
        /// 后台菜单
        /// </summary>
        /// <returns></returns>
        public IActionResult GetMenuTreeDS()
        { 
            return Json(menuService.GetAllMenusForDS(CommonHelper.ToInt64(AuthHelper.GetUserid()),0));
        }
        /// <summary>
        /// 获取用户模块
        /// </summary>
        /// <returns></returns>
        public IActionResult GetUserModules() {
            return Json(menuService.GetUserModules(CommonHelper.ToInt64(AuthHelper.GetUserid()), 0));
        }
        /// <summary>
        /// 获取用户模块菜单
        /// </summary>
        /// <param name="id">模块Id</param>
        /// <returns></returns>
        public IActionResult GetUserModuleMenus(long id) {
            return Json(menuService.GetUserModuleMenus(CommonHelper.ToInt64(AuthHelper.GetUserid()), 0, id));
        }
        public IActionResult GetAllMenus() {

            return Json(menuService.GetAllMenus());
        }
        [Permission("Sys.Menu.Edit||Sys.Menu.Add")]
        public IActionResult Edit(int id=0)
        {
            dynamic viewModel = new ExpandoObject();
            if (id == 0)
            { 
                viewModel.title = "添加";
                viewModel.data = new ExpandoObject();
                // 初始化值
                viewModel.data.menu_id = 0;
                viewModel.data.name = "";
                viewModel.data.permission = "";
                viewModel.data.icon = "";
                viewModel.data.url = "";
                viewModel.data.parent_id = 0;
                viewModel.data.sort = 0;
                viewModel.data.menu_type = 0;
                viewModel.data.menu_position = 0;
                viewModel.data.menu_status = 0;
                viewModel.data.remark = "";
                viewModel.data.js_method = "";
            }
            else
            { 
                viewModel.title = "编辑";
                viewModel.data = menuService.Single(@"menu_id,name,icon,url,parent_id,sort,permission,menu_type,menu_position,menu_status,remark,js_method",
                                              new { menu_id = id });
            } 
            return View(viewModel);
        }
        /// <summary>
        /// ajax 保存数据
        /// </summary>
        /// <returns></returns>
        [Permission("Sys.Menu.Edit||Sys.Menu.Add")]
        public IActionResult EditAjax()
        {
            int result = 0;
            long parent_id = CommonHelper.ToInt64(Request.Form["parent_id"]);
            long menu_id = CommonHelper.ToInt64(Request.Form["menu_id"]);
            string name = CommonHelper.ToString(Request.Form["name"]);
            string permission = CommonHelper.ToString(Request.Form["permission"]);
            string icon = CommonHelper.ToString(Request.Form["icon"]);
            string url = CommonHelper.ToString(Request.Form["url"]);
            string remark = CommonHelper.ToString(Request.Form["remark"]);
            int sort = CommonHelper.ToInt(Request.Form["sort"]);
            int menu_type = CommonHelper.ToInt(Request.Form["menu_type"]); // 菜单类型
            int menu_position = CommonHelper.ToInt(Request.Form["menu_position"]);
            int menu_status = CommonHelper.ToInt(Request.Form["menu_status"]);
            string js_method = CommonHelper.ToString(Request.Form["js_method"]); 
            var data = new
            {
                parent_id = parent_id,
                name = name,
                permission = permission,
                icon = icon,
                url = url,
                sort = sort,
                menu_type = menu_type,
                remark = remark,
                menu_status = menu_status,
                menu_position = menu_position,
                js_method = js_method
            };
            if (menu_id ==0)
            {
                result = menuService.InsertWithUser(data);
            }
            else
            {
                result = menuService.UpdateWithUser(data, new { menu_id= menu_id });
            } 
            if (result > 0)
            {
                return GetSuccessResult();
            }
            return GetErrorResult(); 
        }

        [Permission("Sys.Menu.Edit||Sys.Menu.Delete")]
        public IActionResult Delete() {
            long menu_id = CommonHelper.ToInt64(Request.Form["menu_id"].ToString());
            int result = menuService.Delete(new   { menu_id= menu_id });
            if (result > 0)
            {
                return GetSuccessResult();
            }
            return GetErrorResult();
        }

    }
}